﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Model_SSResourceManager
{
    class QueueDataContext : ItemDataContext<Queue, QUEUE>
    {
        public QueueDataContext()
        {
            EntitySetName = "QUEUES";
        }

        public List<Queue> GetActiveQueues()
        {
            List<Queue> _retList = new List<Queue>();

            using (SSRMDBEntities context = new SSRMDBEntities())
            {
                try
                {
                    Queue modelItem;
                    foreach (QUEUE entItem in (from entQueue in context.QUEUES
                                                  where entQueue.FinishTime >= DateTime.Now 
                                                  select entQueue)
                            )
                    {
                        modelItem = new Queue();
                        EntityToModel(entItem, modelItem);
                        _retList.Add(modelItem);
                    }
                }
                catch (Exception ex)
                {
                    OnRaiceDbErrEvent(new DbErrEventArgs(ex.Message));
                }
            }

            return _retList;
        }

        protected override void EntityToModel(QUEUE _entQueue, Queue _Queue)
        {
            _Queue.Id = _entQueue.Id;
            _Queue.IdResource = _entQueue.IdResource;
            _Queue.StartTime = _entQueue.StartTime;
            _Queue.FinishTime  = _entQueue.FinishTime;
            _Queue.IdUser = _entQueue.IdUser;
            _Queue.MessageTime = _entQueue.MessageTime;
        }

        protected override void ModelToEntity(Queue _Queue, QUEUE _entQueue)
        {
            _entQueue.Id = _Queue.Id;
            _entQueue.IdResource = _Queue.IdResource;
            _entQueue.StartTime = _Queue.StartTime;
            _entQueue.FinishTime = _Queue.FinishTime;
            _entQueue.IdUser = _Queue.IdUser;
            _entQueue.MessageTime = _Queue.MessageTime;
        }
    }
}
